<?php

function getHari($tanggal) {
    if ($tanggal == '1') {
        return "Senin";
    } else if ($tanggal == '2') {
        return "Selasa";
    } else if ($tanggal == '3') {
        return "Rabu";
    } else if ($tanggal == '4') {
        return "Kamis";
    } else if ($tanggal == '5') {
        return "Jumat";
    } else if ($tanggal == '6') {
        return "Sabtu";
    } else if ($tanggal == '0') {
        return "Minggu";
    }
}

$N["N"] = true;
$T["T"] = true;
$S["S"] = true;
$I["I"] = true;
$C["C"] = true;
$M["M"] = true;
$D["D"] = true;

?>

<div id="content">
    <div class="DivTable">
        <?= form_open("", "method=get") ?>
        <table>
            <tr>
                <td>Tahun/Bulan</td>
                <td>:</td>
                <td>
                    <?= form_dropdown("tahun", $list_tahun, $tahun, "id=tahun") . "&nbsp;/&nbsp;" .
                            form_dropdown("bulan", $list_bulan, $bulan, "id=bulan")
                            ?>
                </td>
            </tr>
            <tr>
                <td>Group Kapal</td>
                <td>:</td>
                <td>
                    <?= form_dropdown("vessel", $list_vessel, $vessel, "onchange=\"populate_karyawan(this.value)\"") ?>
                </td>
            </tr>
            <tr>
                <td>Karyawan</td>
                <td>:</td>
                <td>
                    <?= form_dropdown("nama", $list_karyawan, $nama) ?>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>
                    <?= form_submit(array("name" => "btnSubmit"), "Cari") ?>
                </td>
            </tr>
        </table>
        <?= form_close() ?>
    </div>
    <br/>
    <div class="DivTable">
        <div style="float: right">            
            |
            <a style="margin: 2px 3px 4px; cursor: pointer;" title="Lihat Perhitungan Gaji" onclick="show_gaji('<?= $tahun ?>', '<?= $bulan ?>', '<?= $nama ?>');">Hitung Gaji</a>  |            
            <a style="margin: 2px 3px 4px; cursor: pointer;" target='_blank' title="Lihat Data Hutang" href="<?php echo base_url("index.php/hutang/data_hutang");?>">Data Hutang</a> |
            <a style="margin: 2px 3px 4px; cursor: pointer;" target='_blank' title="Lihat Data Hutang" href="<?php echo base_url("index.php/karyawan/rekap_absensi");?>">Rekap Absensi</a>  |
            <a style="margin: 2px 3px 4px; cursor: pointer;" target='_blank' title="Lihat Data Hutang" href="<?php echo base_url("index.php/karyawan/rekap_gaji_abk");?>">Rekap Gaji ABK</a>  |
            <a style="margin: 2px 3px 4px; cursor: pointer;" target='_blank' title="Lihat Data Hutang" href="<?php echo base_url("index.php/master/setting_libur");?>">Setting Libur</a>  |
            <a style="margin: 2px 3px 4px; cursor: pointer;" target='_blank' title="Lihat Data Hutang" href="<?php echo base_url("index.php/master/data_prosesentase_lembur");?>">Setting Prosentase Lembur</a> |
        </div>
        <table class="GridTable" style="width: 100%;">
            <thead>
                <tr>
                    <th rowspan="2">Tanggal</th>
                    <th rowspan="2">Hari</th>
                    <th colspan="8">Keterangan</th>
                </tr>
                <tr>
                    <th>Normal (<span id="sum_normal">0</span>)</th>
                    <th>Terlambat (<span id="sum_terlambat">0</span>)</th>
                    <th>Sakit (<span id="sum_sakit">0</span>)</th>
                    <th>Ijin (<span id="sum_ijin">0</span>)</th>
                    <th>Cuti (<span id="sum_cuti">0</span>)</th>
                    <th>Mangkir (<span id="sum_mangkir">0</span>)</th>
                    <th>Dinas (<span id="sum_dinas">0</span>)</th>
                    <th>Lembur (<span id="sum_lembur">0</span>)</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if ($numrows > 0) {
                    foreach ($result as $row) {
                        $name = "keterangan_" . $row->nik . "_" . $row->tanggal;
                        $class_css = "GridItem2";
                        if ($row->is_libur) {
                            $class_css = "GridItem1";
                        }
                        ?>
                <tr class="<?=$class_css;?>">
                    <td><?= $row->tanggal ?></td>
                    <td><?=getHari(date("w", strtotime($row->tanggal)))?></td>
                    <td><?= form_radio(array("name" => $name, "class" => "normal"), "N", $N[$row->keterangan], "onclick=\"update_absen('$row->nik', '$row->tanggal', 'N');\"") ?></td>
                    <td><?= $row->is_libur ? "" : form_radio(array("name" => $name, "class" => "terlambat"), "T", $T[$row->keterangan], "onclick=\"update_absen('$row->nik', '$row->tanggal', 'T');\"") ?></td>
                    <td><?= $row->is_libur ? "" : form_radio(array("name" => $name, "class" => "sakit"), "S", $S[$row->keterangan], "onclick=\"update_absen('$row->nik', '$row->tanggal', 'S');\"") ?></td>
                    <td><?= $row->is_libur ? "" : form_radio(array("name" => $name, "class" => "ijin"), "I", $I[$row->keterangan], "onclick=\"update_absen('$row->nik', '$row->tanggal', 'I');\"") ?></td>
                    <td><?= $row->is_libur ? "" : form_radio(array("name" => $name, "class" => "cuti"), "C", $C[$row->keterangan], "onclick=\"update_absen('$row->nik', '$row->tanggal', 'C');\"") ?></td>
                    <td><?= $row->is_libur ? "" : form_radio(array("name" => $name, "class" => "mangkir"), "M", $M[$row->keterangan], "onclick=\"update_absen('$row->nik', '$row->tanggal', 'M');\"") ?></td>
                    <td><?= form_radio(array("name" => $name, "class" => "dinas"), "D", $D[$row->keterangan], "onclick=\"update_absen('$row->nik', '$row->tanggal', 'D');\"") ?></td>
                    <td>
                                <?= form_checkbox(array("name" => $name, "class" => "lembur"), $row->prosentase_gaji_pokok, $row->tipe_lembur ? true : false, "onclick=\"show_lembur('$row->nik', '$row->tanggal', this.checked);\"") ?>
                        <div id="<?= "tipe_" . $row->nik . "_" . $row->tanggal ?>" <?= $row->tipe_lembur ? "" : 'style="display: none;"' ?>>
                                    <?php
                                    $name2 = "tl_" . $row->nik . "_" . $row->tanggal;
                                    foreach ($list_lembur as $row2) {
                                        echo form_radio(array("data-to-prop" => $row2->prosentase_gaji_pokok, "name" => $name2), $row2->id_tipe, $row2->id_tipe == $row->tipe_lembur, "onclick=\"update_lembur('$row->nik', '$row->tanggal', '$row2->id_tipe'); update_prop('$row->nik', '$row->tanggal', this);\"");
                                        echo $row2->catatan . "($row2->prosentase_gaji_pokok %)<br/>";
                                    }
                                    ?>
                        </div>
                    </td>
                </tr>
                        <?php
                    }
                } else {
                    ?>
                <tr>
                <tr class="GridItem1" colspan="10">Tidak ada data</tr>
                </tr>
                    <?php } ?>
            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">
    resume("normal");
    resume("terlambat");
    resume("sakit");
    resume("ijin");
    resume("cuti");
    resume("mangkir");
    resume("dinas");
    resume("lembur");

    function get_elm_html(name) {
        return jQuery("#" + name).html();
    }

    function get_pros_lembur() {
        var total = 0;

        jQuery(".lembur:checked").each(function() {
            total += parseFloat(this.value);
        });

        return total;
    }

    function update_absen(nik, tanggal, keterangan) {
        jQuery.ajax({
            type: "POST",
            url: "<?php echo site_url('karyawan/update_absen') ?>",
            data: {nik: nik, tanggal: tanggal, keterangan: keterangan},
            async: false,
            success: function(msg) {
                try {
                    var result = jQuery.parseJSON(msg);
                    if (result.error) {
                        alert(result.msg);
                        jQuery("input[name=keterangan_" + nik + "_" + tanggal + "]").each(
                        function() {
                            if (this.value == result.last) {
                                this.checked = true;
                            } else
                                this.checked = false;
                        }
                    );
                    } else {
                        jQuery("input[name=keterangan_" + nik + "_" + tanggal + "]").each(
                        function() {
                            if (this.value == keterangan || this.value == result.last) {
                                resume(this.className);
                            }
                        }
                    );
                    }
                } catch (e) {
                    alert(e + " Respon dari server tidak dikenali");
                }
            },
            error: function(msg) {
                alert(msg);
                location.reload();
            }
        });
    }

    function show_lembur(nik, tanggal, checked) {
        if (checked) {
            jQuery("#tipe_" + nik + "_" + tanggal).show(300);
        } else {
            jQuery("#keterangan_" + nik + "_" + tanggal).html("");
            update_lembur(nik, tanggal, 0);

            jQuery("#tipe_" + nik + "_" + tanggal).hide(300);
            jQuery("input[name=tl_" + nik + "_" + tanggal + "]").each(function() {
                this.checked = false;
            });
        }
    }

    function update_lembur(nik, tanggal, status) {
        jQuery.ajax({
            type: "POST",
            url: "<?php echo site_url('karyawan/update_lembur') ?>",
            data: {nik: nik, tanggal: tanggal, status: status},
            async: false,
            success: function(msg) {
                try {
                    var result = jQuery.parseJSON(msg);
                    if (result.error) {
                        alert(result.msg);
                    }
                } catch (e) {
                    alert(e + " Respon dari server tidak dikenali");
                }
            },
            error: function(msg) {
                alert(msg);
                location.reload();
            }
        });

        resume('lembur');
    }

    function update_prop(nik, tanggal, elm) {
        jQuery("input[name=keterangan_" + nik + "_" + tanggal + "]").each(
        function() {
            if (this.className == 'lembur') {
                this.value = jQuery(elm).attr("data-to-prop");
            }
        }
    );
    }

    function resume(cls) {
        var length = jQuery("." + cls + ":checked").length;
        jQuery("#sum_" + cls).html(length);
    }

    function show_gaji(tahun, bulan, nik) {
        return GB_showCenter('Data Gaji ' + nik + ' ' + bulan + '/' + tahun, '<?php echo base_url(); ?>index.php/karyawan/index_gaji/' + tahun + '/' + bulan + '/' + nik, 640, 1024);
    }

    function print_gaji(tahun, bulan, nik) {
        return GB_showCenter('Slip Gaji ' + nik + ' ' + bulan + '/' + tahun, '<?php echo base_url(); ?>index.php/karyawan/print_gaji/' + tahun + '/' + bulan + '/' + nik, 600, 1024);
    }

    function show_hutang(nik) {
        return GB_showCenter('Data Hutang ' + nik, '<?php echo base_url(); ?>index.php/karyawan/index_hutang/' + nik, 600, 1024);
    }

    function show_rekap_gaji(tahun, nik) {
        return GB_showCenter('Rekap Gaji ' + nik + ' tahun ' + tahun, '<?php echo base_url(); ?>index.php/karyawan/index_report/' + tahun + '/' + nik, 600, 1024);
    }

    function populate_karyawan(val){
        jQuery.ajax({
            type: "POST",
            async: false,
            url : "<?php echo site_url('karyawan/list_karyawan') ?>",
            data: {vessel: val},
            success: function(msg){
                if(msg != ""){
                    jQuery("select[name=nama]").empty();
                    jQuery("select[name=nama]").append(msg);
                }
            },
            error: function(msg){
                alert(msg.toString());
            }
        }); 
    }
</script>